<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\common\model\learn\book;

use cores\BaseModel;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;

/**
 * 
 * Class Note
 * @package app\common\model
 */
class Note extends BaseModel {
	// 定义表名
	protected $name = 'learn_book_note';

	// 定义主键
	protected $pk = 'note_id';
	// 追加字段
	protected $append = ['show_time'];

	public function getShowTimeAttr($value, $data) {
		$time = Date('Y-m-d',$data['create_time']);
		
		return $time;
	}
	/**
	 * 所属订单
	 * @return BelongsTo
	 */
	public function orderData(): BelongsTo {
		return $this->belongsTo('Order');
	}

	/**
	 * 订单商品
	 * @return BelongsTo
	 */
	public function orderGoods(): BelongsTo {
		return $this->belongsTo('OrderGoods')
			->field(['order_goods_id', 'goods_id', 'goods_name', 'image_id', 'goods_props', 'order_id']);
	}

	/**
	 * 关联用户表
	 * @return BelongsTo
	 */
	public function user(): BelongsTo {
		$model = "app\\common\\model\\User";
		return $this->belongsTo($model)->field(['user_id', 'nick_name', 'avatar_id']);
	}

	

	/**
	 * 关联评价图片表
	 * @return HasMany
	 */
	public function images(): HasMany {
		return $this->hasMany('NoteImage')->order(['id']);
	}

	/**
	 * 详情记录
	 * @param int $NoteId
	 * @param array $with
	 * @return static|array|null
	 */
	public static function detail(int $NoteId, array $with = []) {
		return static::get($NoteId, $with);
	}

	/**
	 * 添加评论图片
	 * @param array $images
	 * @return bool|false
	 */
	protected function addNoteImages(array $images): bool {
		$data = array_map(function ($imageId) {
			return [
				'image_id' => $imageId,
				'store_id' => self::$storeId,
			];
		}, $images);
		return $this->image()->saveAll($data) !== false;
	}
}
